package utils
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	/**
	 * 屏蔽词汇过滤
	 * @author rq_gao
	 * @整理 : 青竹.
	 * 20100324
	 */
	public class LeachDirty
	{
		//private static var dirtyVec:Vector.<RegExp>;
		private static var dirty:Array;
		public function LeachDirty()
		{
		}
		/**
		 * 初始化
		 * @param url 词典url
		 *
		 */
		public static function init(url:String):void
		{
			if(null == dirty)
			{
				var loader:URLLoader = new URLLoader();
				loader.addEventListener(Event.COMPLETE, completeHandler);
				try{loader.load(new URLRequest(url))}catch(e:Error){};
			}
		}
		private static function completeHandler(e:Event):void
		{
			dirty = new Array();
			var dirtyArr:Array = String(URLLoader(e.currentTarget).data).split("|");
			var tmp:*;
			for(var i:int = dirtyArr.length - 1; i>=0; i--)
			{
				tmp = dirtyArr[i];
				if(tmp)
				{
					if(getStringByteLength(tmp)>= String(tmp).length * 3)
					tmp = new RegExp(getRegStr(tmp));
					dirty.push(tmp);
				}
			}
		}
		/**
		 * 过滤
		 * @param v 需过滤字符串
		 * @param replaceStr 替换关键字的字符串
		 * @return 过滤后字符串
		 *
		 */
		public static function leach(v:String,replaceStr:String = "*"):String
		{
			if(dirty)
			{
				var item:*;
				for each(item in dirty)
				{
					//trace("grq0325:" + item + "&" + item.length + "&" + getStringByteLength(item) );
					if(item is String)
					{
						do
						{
							v = v.replace(item,replaceStr);
						}while(v.indexOf(item)>0);
					}
					else
					{
						do
						{
							v = v.replace(item,replaceStr);
						}while(v.search(item)>0);
					}
				}
			}
			return v;
		}

		/**
		 * 检查是否有非法字符。
		 * @param v
		 * @return
		 *
		 */
		public static function check(v:String):Boolean{
			if(dirty)
			{
				var item:*;
				for each(item in dirty)
				{
					if(item is String)
					{
						if(v.indexOf(item)>=0)
						{
							return true
						}
					}else{
						if(v.search(item)>=0)
						{
							return  true
						}
					}
				}
			}
			return false;
		}
		private static function getRegStr(v:String):String
		{
//			var strArr:Array = v.split("");
//			/*var rtn:String = strArr.shift();
//			var len:int = strArr.length;
//			for(var i:int = 0; i < len; i++)
//			{
//				//rtn += "[^\\u9FBF-\\u9FBF]{0,5}" + strArr.shift();
//				rtn += "[\\x20-\\x7F]{0,5}" + strArr.shift();
//			}*/
//			var len:int = strArr.length;
//			var rtn:String = strArr[0];
//			for(var i:int = 1; i < len; i++)
//			{
//				//rtn += "[^\\u9FBF-\\u9FBF]{0,5}" + strArr.shift();
//				rtn += "[\\x20-\\x7F]{0,5}" + strArr[i];
//			}
			var strArr:Array = v.split("");
			var rtn:String = strArr.join("[\\x20-\\x7F]{0,5}");
			//var rtn:String = v.replace(/\B/g,"[\\x20-\\x7F]{0,5}");

			return rtn;
		}
		private static function getStringByteLength(v:String):uint
		{
			var tmp:ByteArray = new ByteArray();
			tmp.writeUTFBytes(v);
			return tmp.length;
		}
		public static function get isReady():Boolean
		{
			return null != dirty;
		}

	}
}